package projectsupporter.common.formbase;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

import projectsupporter.common.control.Path;
import projectsupporter.common.logic.PlugLogic;

public class ProjectChooseDialog {

	private Shell parentShell;
	
	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private Composite composite = null;
	private Label label = null;
	private Button browseButton = null;
	private Text text = null;
	
	private String prjName;

	private Label label1 = null;

	private Button okButton = null;

	public ProjectChooseDialog()
	{
		
	}
	
	public ProjectChooseDialog(Shell parentShell)
	{
		this.parentShell = parentShell;
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		if(parentShell==null)
		{
			sShell = new Shell();
		}
		else
		{
			sShell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		}
		sShell.setText("Project Choose");
		createComposite();
		sShell.setSize(new Point(426, 177));
		sShell.setLayout(new FillLayout());
		sShell.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
			public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
				prjName=null;
			}
		});
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		composite = new Composite(sShell, SWT.NONE);
		composite.setLayout(null);
		label = new Label(composite, SWT.NONE);
		label.setText("Project : ");
		label.setBounds(new Rectangle(17, 40, 54, 18));
		text = new Text(composite, SWT.BORDER);
		text.setBounds(new Rectangle(75, 38, 227, 24));
		browseButton = new Button(composite, SWT.NONE);
		browseButton.setText("Browse");
		browseButton.setLocation(new Point(307, 36));
		browseButton.setSize(new Point(87, 28));
		browseButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						doChoose();
					}
				});
		label1 = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		label1.setBounds(new Rectangle(3, 77, 405, 18));
		label1.setText("Label");
		okButton = new Button(composite, SWT.NONE);
		okButton.setText("OK");
		okButton.setSize(new Point(87, 28));
		okButton.setLocation(new Point(308, 98));
		okButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				closeDialog();
			}
		});
	}
	
	protected void doChoose() {
		FolderFromWorkspaceDialog fd = new FolderFromWorkspaceDialog(sShell);
		fd.setPrjOnly(true);
		Path pt = fd.openFolderSelectDialog();	
		if(pt!=null)
		{
			prjName=pt.getDisplayPath().trim();
			this.text.setText(prjName);
		}
		
	}

	/**
	 * openDialog
	 */
	public String openDialog() {
		final Display display = Display.getDefault();
		createSShell();// 初始化文件夹选择对话框
		sShell.open();// 打开文件夹选择窗口
		sShell.layout();
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
//		display.dispose();
		
		return prjName;
	}

	/**
	 * 关闭对话框
	 */
	protected void closeDialog() {
		if (sShell != null)
			sShell.dispose();// 释放底层的资源
	}
	/**
	 * Dialog test
	 */
	public static void main(String[] args) {
		ProjectChooseDialog fd = new ProjectChooseDialog(null);
		//fd.setPrjOnly(true);
		fd.openDialog();
	}
}
